package com.lsq.tomcat;

import org.apache.catalina.servlets.DefaultServlet;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * @author lsq
 * @description TODO
 * @date 2023/9/14
 */
public class SocketProcess implements Runnable {
    private Socket socket;
    private Tomcat tomcat;

    public SocketProcess(Socket socket, Tomcat tomcat) {
        this.socket = socket;
        this.tomcat = tomcat;
    }

    @Override
    public void run() {
        processSocket(socket);
    }

    private void processSocket(Socket socket) {
        //处理socket 读取与写
        try {
            InputStream inputStream = socket.getInputStream();

            byte[] bytes = new byte[1024];
            inputStream.read(bytes);

            //解析字节流
            int pos = 0;
            int begin = 0, end = 0;
            for (; pos < bytes.length; pos++, end++) {
                if (bytes[pos] == ' ') break;
            }
            //组合空格之前的字节流，转换字符串为方法
            StringBuilder method = new StringBuilder();
            for (; begin < end; begin++) {
                method.append((char) bytes[begin]);
            }
            //解析url
            pos++;
            begin++;
            end++;
            for (; pos < bytes.length; pos++, end++) {
                if (bytes[pos] == ' ') break;
            }
            //组合空格之前的字节流，转换字符串为url
            StringBuilder url = new StringBuilder();
            for (; begin < end; begin++) {
                url.append((char) bytes[begin]);
            }

            //解析协议版本
            pos++;
            begin++;
            end++;
            for (; pos < bytes.length; pos++, end++) {
                if (bytes[pos] == '\r') break;
            }
            //组合空格之前的字节流，转换字符串为url
            StringBuilder protocl = new StringBuilder();
            for (; begin < end; begin++) {
                protocl.append((char) bytes[begin]);
            }

            Request request = new Request(socket, url.toString(), protocl.toString(), method.toString());
            Response response = new Response(request);
            String requestUrl = request.getRequestURL().toString();
            requestUrl = requestUrl.substring(1);
            String[] split = requestUrl.split("/");
            String appName = split[0];
            Context context = tomcat.getContextMap().get(appName);
            if (split.length > 1) {
                Servlet servlet = context.getUrlPattern(split[1]);
                if (servlet != null) {
                    servlet.service(request, response);
                    //匹配servlet,doget
                    //LsqServlet lsqServlet = new LsqServlet();
                    //lsqServlet.service(request, response);

                    //发起相应体
                    response.complete();
                } else {
                    DefaultServlet defaultServlet = new DefaultServlet();
                    defaultServlet.service(request, response);
                    response.complete();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }


    }
}
